<?php
/**
 * Created by PhpStorm.
 * User: yeshen
 * Date: 2021/12/18
 * Time: 10:24
 */
namespace library;

use function Ramsey\Uuid\v1;

class ye_index
{
    //加载框架核心
    public static function run(){
        self::require_vendor();
        self::autoload();
        self::action();
    }



    //引入composer文件
    public static function require_vendor(){
        $vendor_path =  __DIR__."/../vendor/autoload.php";
        require_once $vendor_path;
    }


    /**
     * 加载运行框架
     */
    public static function action(){
        $class    = $_GET['ct']?$_GET['ct']:'index';    //接收的 controller
        $function = $_GET['ac']?$_GET['ac']:'index';    //接收的 function

        //判断类和方法是否存在
        if(!class_exists($class))
        {
            $class = "ctl_".strtolower($class);
            $include_file = __DIR__."/../app/controller/{$class}.php";
            if(is_file($include_file))
            {
                require_once $include_file;
            }
            if(!class_exists($class) || !method_exists($class, $function))
            {
                $code = 404;
                $msg = "class $class or method $function not found";
                self::error($code,$msg);
            }
        }

        unset($_GET['ac']);
        unset($_GET['ct']);

        // 调用类的方法
        try
        {
            $model = new $class();

            // 加载前置方法
            if (method_exists($class, "__before")){
                $model->__before();
            }

            $model->$function();

            // 加载后置方法
            if (method_exists($class, "__after")){
                $model->__after();
            }
        }
        // 有异常
        catch(\Exception $e)
        {
            $code = $e->getCode() ? $e->getCode() : 500;
            self::error($code,$e->getMessage(),$e);
        }
    }


    /**
     * 自动加载
     */
    public  static function autoload(){
         require_once __DIR__."/common.php";

         // controller 自动加载
         $controller_path = __DIR__."/../app/controller";
         common::open_autoload_dir($controller_path,"controller");

         // model 自动加载
         $model_path = __DIR__."/../app/model";
         common::open_autoload_dir($model_path,"model");

         // library 自动加载
         self::autoload_library();

         // config 自动加载
         $config_path = __DIR__."/../config";
         common::open_autoload_dir($config_path,"../config");

         // common 自动加载
         $common_path = __DIR__."/../app/common";
         common::open_autoload_dir($common_path,"common");
    }


    /**
     * library 自动加载
     */
    public static function autoload_library(){
        $library_path = __DIR__."/library.php";
        $librarys     = require_once $library_path;
        foreach ($librarys as $library){
            $library_path = __DIR__."/{$library}/ye_{$library}.php";
            if (is_file($library_path)){
                require_once $library_path;
            }
        }
    }



    public static function error($code,$msg,$data = []){

    }


}